<?php

namespace App\Http\Controllers\Api;

use App\Integral;
use App\Signin;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class SigninController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if (Signin::where('user_id', Auth::id())->whereDate('created_at', Carbon::now()->format('Y-m-d'))->first()) {
            return response(['message' => '已签过'], 400);
        }
        Integral::create([
            'user_id' => Auth::id(),
            'type' => 1,
            'number' => 5,
            'description' => '每日签到'
        ]);
        $user = Auth::user();
        $user->integral = $user->integral + 5;
        $user->save();
        return Signin::create(['user_id'=> Auth::id()]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Signin  $signin
     * @return \Illuminate\Http\Response
     */
    public function show(Signin $signin)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Signin  $signin
     * @return \Illuminate\Http\Response
     */
    public function edit(Signin $signin)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Signin  $signin
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Signin $signin)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Signin  $signin
     * @return \Illuminate\Http\Response
     */
    public function destroy(Signin $signin)
    {
        //
    }
}
